package com.jdone.compus.service;

import com.jdone.compus.model.BlowDevice;
import com.jdone.compus.model.BlowDeviceRepair;
import com.jdone.compus.repository.BlowDeviceRepairRepository;
import com.jdone.compus.repository.BlowDeviceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.time.LocalDateTime;

@Service
public class BlowDeviceRepairService {

    @Autowired
    private BlowDeviceRepairRepository blowDeviceRepairRepository;

    @Autowired
    private BlowDeviceRepository blowDeviceRepository;

    public BlowDeviceRepair addRepairRecord(Long deviceId, String repairName, String repairAccount,
                                            String repairType, String repairPhone, String repairNotes, String images) {
        // 查找设备
        BlowDevice blowDevice = blowDeviceRepository.findById(deviceId)
                .orElseThrow(() -> new RuntimeException("设备未找到"));

        // 更新设备状态为维修中
        blowDevice.setStatus(BlowDevice.DeviceStatus.DAMAGED);
        blowDeviceRepository.save(blowDevice);

        // 创建保修记录
        BlowDeviceRepair repairRecord = new BlowDeviceRepair();
        repairRecord.setBlowDevice(blowDevice);
        repairRecord.setRepairName(repairName);
        repairRecord.setRepairAccount(repairAccount);
        repairRecord.setRepairType(repairType);
        repairRecord.setRepairPhone(repairPhone);
        repairRecord.setRepairTime(LocalDateTime.now());
        repairRecord.setRepairNotes(repairNotes);
        repairRecord.setImages(images);
        repairRecord.setCreatedAt(LocalDateTime.now());
        repairRecord.setUpdatedAt(LocalDateTime.now());
        repairRecord.setRepairStatus(0);

        // 保存保修记录
        return blowDeviceRepairRepository.save(repairRecord);
    }


    /**
     * 分页查询所有维修记录
     *
     * @param pageable 分页对象
     * @return 分页结果
     */
    public Page<BlowDeviceRepair> getAllRepairRecords(Pageable pageable) {
        return blowDeviceRepairRepository.findAll(pageable);
    }


    /**
     * 设备修复
     *
     * @param repairId
     * @return
     */
    public BlowDeviceRepair updateRepairRecord(Long repairId) {
        // 查找维修记录
        BlowDeviceRepair repairRecord = blowDeviceRepairRepository.findById(repairId)
                .orElseThrow(() -> new RuntimeException("维修记录未找到"));

        // 获取关联的设备
        BlowDevice blowDevice = repairRecord.getBlowDevice();

        // 将设备状态修改为正常
        blowDevice.setStatus(BlowDevice.DeviceStatus.NORMAL);
        blowDeviceRepository.save(blowDevice);

        //把维修记录设置为已处理状态
        repairRecord.setRepairStatus(1);
        blowDeviceRepairRepository.save(repairRecord);

        // 返回更新后的维修记录
        return repairRecord;
    }

    /**
     * 删除维修记录
     *
     * @param repairId
     */
    public void deleteRepairRecord(Long repairId) {
        BlowDeviceRepair repairRecord = blowDeviceRepairRepository.findById(repairId)
                .orElseThrow(() -> new RuntimeException("维修记录未找到"));

        // 删除维修记录
        blowDeviceRepairRepository.delete(repairRecord);
    }

}

